package com.xmx.ndmallwxapi.service;



import com.xmx.common.utils.ListFromEntity2VoUtils;
import com.xmx.common.utils.R;
import com.xmx.ndmallwxapi.feign.CouponFeignClient;
import com.xmx.ndmallwxapi.feign.MallFeignClient;
import com.xmx.ndmallwxapi.feign.ProductFeignClient;
import com.xmx.ndmallwxapi.feign.PromotionFeignClient;
import com.xmx.ndmallwxapi.vo.brand.BrandHomeVo;
import com.xmx.ndmallwxapi.vo.category.CategoryHomeVo;
import com.xmx.ndmallwxapi.vo.category.FloorHomeVo;
import com.xmx.ndmallwxapi.vo.copon.CouponVo;
import com.xmx.ndmallwxapi.vo.goods.NewGoodsVo;
import com.xmx.ndmallwxapi.vo.mall.TopicVo;
import com.xmx.ndmallwxapi.vo.promotion.AdVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

/**

 * @Date: 2021/8/11 10:52
 */
@Service
@Slf4j
public class IndexServie2 {

    @Resource
    private ProductFeignClient productFeignClient;

    @Resource
    private CouponFeignClient couponFeignClient;

    @Resource
    private PromotionFeignClient promotionFeignClient;

    @Resource
    private MallFeignClient mallFeignClient;


//    写入缓存
    @Cacheable(cacheNames = "indexdata",key = "'name:findIndex'")
//    清楚缓存
//    @CacheEvict(cacheNames = "indexdate",allEntries=true)
//    更新缓存
//    @CachePut(cacheNames = "indexdata")
    public Map findIndexData() {

        Map resultMap = new HashMap<String, List>();
        //TODO 一、营销推广微服务
        //1.banner（3个）
        listbanner(resultMap);

        //2.grouponList（4个）
        listGoupon(resultMap);

        //TODO 二、优惠券微服务
        //3.couponList（4个）
        listCoupon(resultMap);

        //TODO 三、 商品微服务
        listProduct(resultMap);

        //TODO 四、商城微服务服务
        //1.topicList（4个）
        listmall(resultMap);
        return resultMap;
    }

    private void listProduct(Map resultMap) {
        //TODO 1.channel（10个）
        Map paramMap = new HashMap<String, String>();
        paramMap.put("sort", "add_time");
        paramMap.put("order", "desc");

        R channelR = productFeignClient.listChannel(paramMap);
        List<Map> productList = (List<Map>) ((Map) channelR.get("page")).get("list");
        List<CategoryHomeVo> categoryHomeVoList = ListFromEntity2VoUtils.entity2Vo(productList, CategoryHomeVo.class);
        resultMap.put("channel", categoryHomeVoList);
        //TODO 2.brandList（4个）
        paramMap.clear();

        paramMap.put("limit", 4);
        R brandR = productFeignClient.listBrand(paramMap);
        List<Map> brandList = (List<Map>) ((Map) brandR.get("page")).get("list");
        List<BrandHomeVo> brandHomeVoList = ListFromEntity2VoUtils.entity2Vo(brandList, BrandHomeVo.class);
        resultMap.put("brandList", brandHomeVoList);
        //TODO 3.newGoodsList（6个）----本来：营销库中获取   现在：产库库中获取
        paramMap.clear();

        paramMap.put("limit", 6);
        paramMap.put("sort", "add_time");
        paramMap.put("order", "desc");
        paramMap.put("isNew", "1");//1是新品 0是不是新品
        R newGoodsR = productFeignClient.listNewGoods(paramMap);
        List<Map> newGoodsList = (List<Map>) ((Map) newGoodsR.get("page")).get("list");
        List<NewGoodsVo> newGoodsVoList = ListFromEntity2VoUtils.entity2Vo(newGoodsList, NewGoodsVo.class);
        resultMap.put("newGoodsList", newGoodsVoList);
        //TODO 4.hotGoodsList（6个）
        paramMap.clear();
        paramMap.put("limit", 6);
        paramMap.put("sort", "add_time");
        paramMap.put("order", "desc");
        paramMap.put("isHot", "1");//1是新品 0是不是新品
        R hotGoodsR = productFeignClient.listNewGoods(paramMap);
        List<Map> hotGoodsList = (List<Map>) ((Map) hotGoodsR.get("page")).get("list");
        List<NewGoodsVo> hotGoodsVoList = ListFromEntity2VoUtils.entity2Vo(hotGoodsList, NewGoodsVo.class);
        resultMap.put("hotGoodsList", hotGoodsVoList);

        //TODO 5.floorList（4个分类-4个商品）
        paramMap.clear();
        R floorGoodsR = productFeignClient.getFloorGoodsList();
        List<Map> floorList = (List<Map>) ((Map) floorGoodsR).get("floor");
        List<FloorHomeVo> floorHomeVoList = ListFromEntity2VoUtils.entity2Vo(floorList, FloorHomeVo.class);
        resultMap.put("floorGoodsList",floorHomeVoList);

    }

    private void listCoupon(Map resultMap){
        Map paramMap = new HashMap<String, String>();
        paramMap.put("limit", 2);
        paramMap.put("id", "desc");
        R channelR = couponFeignClient.listcoupon(paramMap);
        List<Map> couponList = (List<Map>) ((Map) channelR.get("page")).get("list");
        List<CouponVo> couponVoList = ListFromEntity2VoUtils.entity2Vo(couponList, CouponVo.class);
        resultMap.put("couponList", couponVoList);
        //TODO 2.brandList（4个）
        paramMap.clear();
    }

    private void listGoupon(Map resultMap){
        //TODO 1.
        R grouponlistR = promotionFeignClient.grouponlist();
        List<Map> couponList = (List<Map>) ((Map) grouponlistR.get("page")).get("list");
        resultMap.put("grouponList", couponList);
    }

    private void listbanner(Map resultMap){
        Map paramMap = new HashMap<String, String>();
        paramMap.put("limit", 3);
        paramMap.put("id", "desc");
        R bannerR = promotionFeignClient.adlist(paramMap);
        List<Map> bannerList = (List<Map>) ((Map) bannerR.get("page")).get("list");
        List<AdVo> AdVoList = ListFromEntity2VoUtils.entity2Vo(bannerList, AdVo.class);
        resultMap.put("banner", AdVoList);
        //TODO 2.brandList（4个）
        paramMap.clear();
    }

    private void listmall(Map resultMap){
        Map paramMap = new HashMap<String, String>();
        paramMap.put("limit", 4);
        paramMap.put("id", "desc");
        R bannerR = mallFeignClient.malllist(paramMap);
        List<Map> topicList = (List<Map>) ((Map) bannerR.get("page")).get("list");
        List<TopicVo> TopicVoList = ListFromEntity2VoUtils.entity2Vo(topicList, TopicVo.class);
        resultMap.put("topicList", TopicVoList);
        //TODO 2.brandList（4个）
        paramMap.clear();
    }
}
